{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modules and Packages "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# WAP to read excel file\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting excel\n",
      "  Downloading excel-1.0.0.tar.gz (2.3 kB)\n",
      "Requirement already satisfied: xlrd in /opt/anaconda3/lib/python3.7/site-packages (from excel) (1.2.0)\n",
      "Building wheels for collected packages: excel\n",
      "  Building wheel for excel (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for excel: filename=excel-1.0.0-py3-none-any.whl size=3007 sha256=5c4016cc219d7b012be5ca5bfa4e9628c2ae79bd54448743593b16224c3d795e\n",
      "  Stored in directory: /Users/nvrsettle/Library/Caches/pip/wheels/40/20/eb/00974261534573b83d9836784d45accfe983fa95804db30832\n",
      "Successfully built excel\n",
      "Installing collected packages: excel\n",
      "Successfully installed excel-1.0.0\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "! pip install excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['No', 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0]\n",
      "['No', 'ID', 'Attempt', 'Candidate Name', 'Username', 'GroupName', 'Exam', 'Exam ID', 'Marks or Points', 'Percentage', 'Result', 'examstatus', 'TimeTaken', 'Start_DateTime', 'Finish_DateTime', 'Mode', 'Email', 'Mobile', 'Certificateid', 'Candidate registered on']\n",
      "[1.0, 8542.0, 1.0, 'Letsupgradeb4fcs050 Letsupgradeb4fcs050', 'LetsUpgradeB4FCS050', 'LetsUpgrade FCS B4', 'ReExam LetsUpgrade FCS Python B4 Exam', 746.0, '22 Out of 91', 24.0, 'Fail', 'Completed', 0.028460648148148148, 44003.47224537037, 44003.500706018516, 'Exam Mode', 'praisey0234@gmail.com', '', 0.0, 43994.318564814814]\n"
     ]
    }
   ],
   "source": [
    "from excel import OpenExcel\n",
    "\n",
    "f = OpenExcel('exam.xls')\n",
    "\n",
    "f.read() # read all\n",
    "\n",
    "print(f.read('A')) # read 'A' row\n",
    "\n",
    "print(f.read(1) )# f.read('1'), read '1' column\n",
    "\n",
    "print(f.read(2)) # read 'A5' position"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating our Own Modules and Packages "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing addition.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile addition.py\n",
    "\n",
    "def addition(*args):\n",
    "    add = 0\n",
    "    for i in args:\n",
    "        add = add + i\n",
    "    return add"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35\n"
     ]
    }
   ],
   "source": [
    "from Calculator import addition\n",
    "\n",
    "temp = addition.addition(4,5,6,7,8,5)\n",
    "print(temp)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting subtraction.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile subtraction.py\n",
    "\n",
    "def sub(num1, num2):\n",
    "    minus = num1 - num2\n",
    "    return minus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "from Calculator import subtraction\n",
    "\n",
    "temp = subtraction.sub(4,2)\n",
    "print(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# __MAIN__ and __NAME___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. YOUR .PY File can work as Module also and can work as Main Code also ?\n",
    "\n",
    "2. If you want to know is my .py file is running as a Module or it is running as a Main Code ?\n",
    "\n",
    "3. That time you use __main__ == __name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting file1.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile file1.py\n",
    "\n",
    "print(\"This is File1.py\")\n",
    "\n",
    "if __name__ == \"__main__\": # This is a Condition to check weather the \n",
    "    #file is called directly or indirectly\n",
    "    print(\"This File is running directly\")\n",
    "else:\n",
    "    print(\"This file is running Indirectly as Module\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is File1.py\r\n",
      "This File is running directly\r\n"
     ]
    }
   ],
   "source": [
    "! python file1.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing file2.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile file2.py\n",
    "\n",
    "import file1\n",
    "\n",
    "print(\"This is File2.py\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(\"This is running directly\")\n",
    "else:\n",
    "    print(\"This is running In-Directly as module\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is File1.py\r\n",
      "This file is running Indirectly as Module\r\n",
      "This is File2.py\r\n",
      "This is running directly\r\n"
     ]
    }
   ],
   "source": [
    "! python file2.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decorators !!!\n",
    "\n",
    "- You all have a some function1\n",
    "\n",
    "- You are required to create one more function2\n",
    "\n",
    "- function2 can be done with few modification in Function1\n",
    "\n",
    "- But you are required to copy paste the whole stuff of function 1 and modify \n",
    "\n",
    "- This will increase the numbers of lines of code in the program \n",
    "\n",
    "- This will actually waste memory the computer \n",
    "\n",
    "- This is a kind of static way of doing \n",
    "\n",
    "- is there any better way ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# basic Function \n",
    "def hi():\n",
    "    print(\"HI Sonali\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI Sonali\n"
     ]
    }
   ],
   "source": [
    "hi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# basic Functuion with arg\n",
    "def hi(arg = \"LetsUpgrader\"):\n",
    "    print(\"HI - \", arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI -  Sonali Agwane\n"
     ]
    }
   ],
   "source": [
    "hi(\"Sonali Agwane\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI -  LetsUpgrader\n"
     ]
    }
   ],
   "source": [
    "hi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# renaming the functionm\n",
    "hello = hi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI -  Music Lover\n"
     ]
    }
   ],
   "source": [
    "hello(\"Music Lover\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can we pass a function as Argument ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Amigo(arg):\n",
    "    print(\"we are inside amigo\")\n",
    "    \n",
    "    arg()\n",
    "    \n",
    "    print(\"We are done executing a function inside a function\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "we are inside amigo\n",
      "HI -  LetsUpgrader\n",
      "We are done executing a function inside a function\n"
     ]
    }
   ],
   "source": [
    "Amigo(hi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can you return a function from the Function ?\n",
    "\n",
    "def hola():\n",
    "    print( \" we will be returning a function from the function \")\n",
    "    \n",
    "    def namaskar(arg):\n",
    "        print(\"Hello - \", arg)\n",
    "    \n",
    "    return namaskar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " we will be returning a function from the function \n"
     ]
    }
   ],
   "source": [
    "abc = hola()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello -  Sai\n"
     ]
    }
   ],
   "source": [
    "abc(\"Sai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can we decorate a function inside a function with other functionality "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "def needRapper():\n",
    "    print(\"Hey I am the Patty i need bread around me\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def wrapperFun(arg):\n",
    "    \n",
    "    def wrappedCode():\n",
    "        print(\"This is Top bread code\")\n",
    "        \n",
    "        arg()\n",
    "        \n",
    "        print(\"This is Bottom bread code\")\n",
    "\n",
    "    return wrappedCode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "burger = wrapperFun(needRapper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is Top bread code\n",
      "Hey I am the Patty i need bread around me\n",
      "This is Bottom bread code\n"
     ]
    }
   ],
   "source": [
    "burger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def addition(num1,num2):\n",
    "    print(\"This is a addition function\")\n",
    "    print(\"Result - \",num1 + num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def subtraction(num1,num2):\n",
    "    print(\"This is a Subtraction function\")\n",
    "    print(\"Result - \", num1 - num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "def inputCode(arg):\n",
    "    def modifiedFunction():\n",
    "        num1 = int(input(\"Enter the Number 1 - \"))\n",
    "        num2 = int(input(\"Enter the Number 2 - \"))\n",
    "        arg(num1,num2)\n",
    "    return modifiedFunction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "fullAdditionFunWithIP = inputCode(addition)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the Number 1 - 56\n",
      "Enter the Number 2 - 12\n",
      "This is a addition function\n",
      "Result -  68\n"
     ]
    }
   ],
   "source": [
    "fullAdditionFunWithIP()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "fullSubtractionFunWithIP = inputCode(subtraction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the Number 156\n",
      "Enter the Number 134\n",
      "This is a Subtraction function\n",
      "Result -  22\n"
     ]
    }
   ],
   "source": [
    "fullSubtractionFunWithIP()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "@inputCode # This is known as Decorator \n",
    "def multipy(num1,num2):\n",
    "    print(num1* num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the Number 1 - 56\n",
      "Enter the Number 2 - 12\n",
      "672\n"
     ]
    }
   ],
   "source": [
    "multipy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generators "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "# How many things can you return from a function ??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isOdd(num):\n",
    "    if num%2 != 0:\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isOdd(45)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this code will return the first ODD and exit \n",
    "def isOdd(range1 = 1, range2 = 10):\n",
    "    for num in range(range1, range2):\n",
    "        if num%2 != 0:\n",
    "            return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "oddNumbers = isOdd(1,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oddNumbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We want our code to have all the odd numbers in the range and o/p\n",
    "def isOdd(range1 = 1, range2 = 10):\n",
    "    for num in range(range1, range2):\n",
    "        if num%2 != 0:\n",
    "            yield num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "oddNumbers = isOdd(1,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]\n"
     ]
    }
   ],
   "source": [
    "print(list(oddNumbers))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
